package com.hucheng.rest.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.page.CommonPager;
import com.hucheng.common.page.PageParameter;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.utils.ValidatorUtils;
import com.hucheng.goods.dto.CartDTO;
import com.hucheng.goods.query.CartQuery;
import com.hucheng.goods.service.CartService;
import com.hucheng.goods.vo.CartVO;
import com.hucheng.rest.base.BaseAction;
import org.springframework.stereotype.Controller;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-17
 * @Description: 购物车
 **/
@Controller("cartAction")
public class CartAction extends BaseAction {

    @Reference
    private CartService cartService;

    public ServiceCoreVO listV100() throws EduException {

        String currentPage = getRequestContentValue("currentPage");
        String pageSize = getRequestContentValue("pageSize");

        PageParameter pageParameter = new PageParameter();
        if (currentPage != null && !currentPage.equals("null") &&
                pageSize != null && !pageSize.equals("null")) {
            pageParameter = new PageParameter(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        }

        CartQuery cartQuery = new CartQuery();
        cartQuery.setUserId(userId);
        cartQuery.setPageParameter(pageParameter);
        CommonPager<CartVO> lists = cartService.cartLists(cartQuery);
        return response(lists);
    }

    public ServiceCoreVO addCartV100() throws EduException {

        String goodsId = getRequestContentValue("goods_id");
        String goodsNumber = getRequestContentValue("goods_number");

        if (ValidatorUtils.isEmpty(goodsNumber)) {
            goodsNumber = "1";
        }

        CartDTO cartDTO = new CartDTO();
        cartDTO.setUserId(userId);
        cartDTO.setGoodsId(Integer.parseInt(goodsId));
        cartDTO.setGoodsNumber(Integer.parseInt(goodsNumber));

        cartService.addCart(cartDTO);

        return response();
    }

    public ServiceCoreVO updateCartV100() {

        return response();
    }

    public ServiceCoreVO delCartV100() throws EduException {

        String cartId = getRequestContentValue("cartId");

        if (ValidatorUtils.isEmpty(cartId)) {
            throw new EduException(170002);
        }

        CommonPager<CartVO> lists = cartService.delCart(Integer.parseInt(cartId), userId);
        return response(lists);
    }

    public ServiceCoreVO cleanCartV100() {

        return response();
    }

}
